<template>
  <el-dialog
    :title="title"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    width="600px"
    :before-close="cancel">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="扣减余额" prop="amount">
        <el-input type="number" v-model.number="form.amount">
          <el-select
            v-if="type == 'auto'"
            style="width: 150px"
            v-model="form.selectType"
            slot="append"
            placeholder="增减选择">
            <el-option label="自由增加" :value="3"></el-option>
            <el-option label="自由减少" :value="4"></el-option>
          </el-select>
        </el-input>
      </el-form-item>
      <el-form-item label="扣减理由" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="7" show-word-limit maxlength="500" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleReduce">确认 </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { handleBalance } from '@/api/bill/platformBalance'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * data
     */
    data: {
      type: Array,
      default: () => [],
    },
    /**
     * 标题
     */
    title: {
      type: String,
      required: true,
    },
    /**
     * type
     */
    type: {
      type: [String, Number],
      required: true,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {},
      form: {
        selectType: 3,
      },
      // 表单校验
      rules: {
        remark: [{ required: true, message: '请输入', trigger: 'blur' }],
        amount: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        if (this.type == 'auto') {
          this.form.selectType = 3
        }
      }
    },
  },
  methods: {
    handleReduce() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          //  * type	类型： 1：消费税扣减 2：运费扣减 3：自由增加 4：自由减少

          if (this.type == '1' || this.type == '2') {
            this.balance(this.type)
          }

          if (this.type == 'auto') {
            this.balance(this.form.selectType)
          }
        }
      })
    },

    balance(type) {
      this.$confirm(this.$t('确认操作?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          type,
          amount: this.form.amount,
          remark: this.form.remark,
        }
        handleBalance(data).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.cancel()
            this.$emit('getList')
          }
        })
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
